package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public Result add(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        dishService.add(dto);
        return Result.success();
    }

    /**
     * 分页查询菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto) {
        log.info("菜品管理分页查询：{}", dto);
        PageResult page = dishService.page(dto);
        return Result.success(page);
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除菜品")
    @DeleteMapping
    public Result deleteByIds(@RequestParam(value = "ids", required = true) List<Long> ids) {
        log.info("删除菜品：{}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 根据id查询菜品
     */
    @ApiOperation(value = "根据id查询菜品")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id) {
        log.info("回显菜品数据：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto) {
        log.info("修改菜品：{}", dto);
        dishService.update(dto);
        return Result.success();
    }

    /**
     * 菜品停售/起售
     */
    @ApiOperation(value = "菜品停售起售")
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status, Long id) {
        log.info("修改菜品停售起售状态：{},{}",id, status == 1 ? "起售" : "停售");
        dishService.status(status, id);
        return Result.success();
    }
}
